package com.wutianyi.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.RandomStringUtils;

public class SessionTestServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5283142856372669464L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/plain");
		PrintWriter pw = resp.getWriter();
		HttpSession httpSession = req.getSession();
		pw.println(httpSession.getId());
		pw.println(httpSession.getCreationTime());
		pw.println(httpSession.getMaxInactiveInterval());

		Enumeration<String> attributeNames = httpSession.getAttributeNames();
		while (attributeNames.hasMoreElements()) {
			String k = attributeNames.nextElement();
			pw.println(k + ": " + httpSession.getAttribute(k));
		}

		httpSession.setAttribute(RandomStringUtils.randomNumeric(20),
				RandomStringUtils.randomAscii(100));
	}

}
